package com.py.androidUtils.base;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.py.androidUtils.base.BaseAction.TaskListener;
import com.py.androidUtils.pulltorefrsh.PullToRefreshBase;
import com.py.androidUtils.pulltorefrsh.PullToRefreshBase.Mode;
import com.py.androidUtils.pulltorefrsh.PullToRefreshBase.OnRefreshListener;
import com.py.androidUtils.pulltorefrsh.PullToRefreshListView;
import com.py.androidUtils.utils.AppUtils;
import com.py.androidUtils.view.LoadDataEemptyView;
import com.py.androidUtils.view.LoadDataEemptyView.IClickLoadListener;
import com.readyidu.app.R;

public abstract class BaseListActivity<T> extends BaseActivity {
	/** 没有刷新和加载 */
	public static final Mode DISABLED = Mode.DISABLED;
	/** 只有刷新 */
	public static final Mode PULL_FROM_START = Mode.PULL_FROM_START;
	/** 只有加载 */
	public static final Mode PULL_FROM_END = Mode.PULL_FROM_END;
	/** 有加载和加载 */
	public static final Mode BOTH = Mode.BOTH;
	/** 没有刷新和加载 */
	public static final Mode MANUAL_REFRESH_ONLY = Mode.MANUAL_REFRESH_ONLY;

	protected BaseCommAdapter<T> commAdapter;
	public PullToRefreshListView listview;
	private int selectPosition = 1;
	private LoadDataEemptyView loadData;
	public BaseAction baseAction;
	public int page = 0;
	private LinearLayout footerLine;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.common_base_list_activity);
		listview = (PullToRefreshListView) this.findViewById(R.id.listView);
		loadData = (LoadDataEemptyView) this.findViewById(R.id.loadData);
		footerLine = (LinearLayout) this.findViewById(R.id.footerView);
		initData();
	}

	private void initData() {
		View headView = getHeadView();
		View footerView = getFooterView();
		onActivityCreatedCallBack();
		if (getListViewMode() != null) {
			listview.setMode(getListViewMode());
		} else {
			listview.setMode(Mode.BOTH);
		}
		listview.getRefreshableView().setAdapter(commAdapter);
		if (headView != null) {
			listview.getRefreshableView().addHeaderView(headView);
			selectPosition = 2;
		}
		if (footerView != null) {
			footerLine.addView(footerView);
		}
		listview.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (listview.isRefreshData()) {
					commAdapter.clear();
					page = 0;
					loadData();
				} else {// 加载更多
					loadData();
				}
			}
		});
		listview.getRefreshableView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				onItemClickCallBack(parent, view, position - selectPosition, id);
			}
		});

		baseAction = new BaseAction(BaseListActivity.this) {
			@Override
			protected void doInbackground() {
				doInbackgroundCallBack();
			}
		};
		baseAction.setTaskListener(new TaskListener() {
			@Override
			public void onPreExecute() {
				onPreExecuteCallBack();
			}

			@Override
			public void onPostExecute() {
				List<T> _list = doHandelData(baseAction.getData());
				if (_list != null && _list.size() > 0) {
					page++;
					loadData.setVisibility(View.GONE);
					listview.setVisibility(View.VISIBLE);
					commAdapter.setList(_list);
				} else {
					loadData.setVisibility(View.VISIBLE);
					listview.setVisibility(View.GONE);
					loadData.loadEmptyError();
				}
				stopRefreshView();
			}
		});
		checkNetWorkLoadData();

		loadData.setClickLoadListener(new IClickLoadListener() {
			@Override
			public void load() {
				checkNetWorkLoadData();
			}

			@Override
			public void settingNetWork() {
				AppUtils.settingNetWork(BaseListActivity.this);
			}
		});
	}

	/**
	 * 检测网络网络连接正常则加载数据
	 */
	public void checkNetWorkLoadData() {
		listview.setVisibility(View.GONE);
		loadData.setVisibility(View.VISIBLE);
		loadData.loadData();
		loadData();
	}

	/**
	 * 请求数据
	 */
	private void loadData() {
		baseAction.execute();
	}

	/**
	 * 结束刷新
	 */
	public void stopRefreshView() {
		listview.onRefreshComplete();
	}

	/** 界面回调 */
	public abstract void onActivityCreatedCallBack();

	/** 数据回调 */
	public abstract void doInbackgroundCallBack();

	public abstract void onPreExecuteCallBack();

	/** 绑定数据 */
	public abstract List<T> doHandelData(Object object);

	/** 列表点击事件 */
	public abstract void onItemClickCallBack(AdapterView<?> parent, View view, int position, long id);

	/** 获取头部View */
	public abstract View getHeadView();

	/** 获取底部View */
	public abstract View getFooterView();

	/** 设置listView的类型 */
	public abstract Mode getListViewMode();
}
